package com.pony.iphone.controller;

import com.pony.iphone.data.FileDB;
import com.pony.iphone.utils.AppHttpCodeEnum;
import com.pony.iphone.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author pony
 * @version 1.0
 * @description: 测试收集邮箱账号使用情况，主要收集邮箱账号的数量，持久化到一个本质文件中
 * @create 2025/8/12 21:38
 */
@RestController
@RequestMapping("account")
public class AccountController {

    @Autowired
    private FileDB fileDb;

    /**
     * 修改收集的邮箱数量信息，存放的地址
     * @param url 上一次存储数据的 地址
     * @return
     */
    @PostMapping("/updateUrl")
    public R updateUrl(@RequestParam String url){
        System.out.println(url);
        String oldUrl = String.valueOf(fileDb.getAbsoluteFilePath());
        fileDb.setRelativeFilePath(url);
        return R.success(AppHttpCodeEnum.SUCCESS, "存放地址，修改成功").data("oldUrl", oldUrl);
    }

    /**
     * 读取文件中的值并且+1后重新写入文件
     * @return 增加后的值
     */
    @PostMapping("/addCount")
    public R addCount(){
        int count = fileDb.incrementAndGet();
        System.out.println(fileDb.getAbsoluteFilePath());
        return R.success(AppHttpCodeEnum.SUCCESS, "+1成功，文件中的当前值为：" + count).data("count", count);
    }

    /**
     * 获取当前一共消耗掉的账号数量
     * @return
     */
    @GetMapping("/getCount")
    public R getCount(){
        int count = fileDb.readNumber();
        return R.success(AppHttpCodeEnum.SUCCESS, "获取当前一共消耗掉的账号数量：" + count).data("count", count);
    }

    /**
     * 清空文件，重置为0
     * @return
     */
    @GetMapping("/resetCount")
    public R resetCount(){
        String oldUrl = String.valueOf(fileDb.getAbsoluteFilePath());
        fileDb.writeNumber(0);
        int count = fileDb.readNumber();
        return R.success(AppHttpCodeEnum.SUCCESS, "统计账号数量，重置成功").data("count", count);
    }
}
